<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Quiz/classes/PhpFox_ComponentQuizList.class.php, $Author:  $ */



/** List of quizzes
 *
 *
 * @version $Id: PhpFox_ComponentQuizList.class.php 416 2008-03-28 07:20:51Z phpfoxsv $
 * @package module.quiz
 */
class PhpFox_Mod_Quiz_ComponentQuizList extends BaseComponent
{
    function process()
    {
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        $oSrvSec->checkPerm('quiz', array('read_only', 'full'));

        $oCurrUser = &$oSrvSec->getCurrentUser();

        $oSrvQuiz = &$this->_oModule->getService('Quiz');
        /* @var $oSrvQuiz PhpFox_Mod_Quiz_ServiceQuiz */

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */

        $oQuiz = $this->_oModule->getItem('Quiz');
        $oQuiz->load($oReq->getInt('quizid'));

        switch($oReq->getAction())
        {
            case 'quiz_delete':
                if ($oSrvSec->isStaff() ||
                    (isset($oCurrUser) && $oCurrUser && ($oQuiz->aData['user'] == $oCurrUser->aData['user'])) )
                {
                    $oSrvQuiz->deleteQuiz($oReq->getInt('quizid'));
                }
                $oUrl = &App::getUrl();
                $oUrl->clearParams('act');
                $oUrl->clearParams('quizid');
                App::forward($oUrl->getUrl());
                break;
        }

        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('public.quiz.pagesize');

        $aCond = array(
            'type = 2', //2 -- hard legacy (user for approved quizzes)
        );
        $sSort = '`time` DESC';
        list($aQuizzes, $iCnt) = $oSrvQuiz->getQuizList($aCond, $iPage, $iPageSize, $sSort);
        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

        $bIsStaff = $oSrvSec->isStaff();
        $bCanTake = ('full' == $oSrvSec->getPerm('quiz'));
        foreach($aQuizzes as $k => $v)
        {
            $aQuizzes[$k]['can_delete'] = ($bIsStaff || (isset($oCurrUser) && $oCurrUser && ($v['user'] == $oCurrUser->aData['user'])));
            $aQuizzes[$k]['can_take']   = $bCanTake;
            $aQuizzes[$k]['is_owner']   = (isset($oCurrUser) && $oCurrUser ? ($v['user'] == $oCurrUser->aData['user']) : false);
            $aQuizzes[$k]['is_taken']   = $oSrvQuiz->isPassedQuiz($oCurrUser, $v['id']);
        }


        $oTpl = &$this->_oModule->getTemplate();
        $oTpl->assign(array(
            'aQuizzes'    => $aQuizzes,
            'sUserPicUrl' => App::getParam('user.pic.url'),
            'aPaging'     => $oPager->getInfo(App::getUrl()),
            'bIsAdmin'    => $oSrvSec->isStaff(),
        ));
        return $oTpl->fetch('QuizList.html');
    }
}
?>